#include "EuclideanClusterExtractionModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.euclidean_cluster_extraction_model.json";
}

EuclideanClusterExtractionModel::EuclideanClusterExtractionModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/cluster_extraction.html");
}

EuclideanClusterExtractionModel& EuclideanClusterExtractionModel::get()
{
    static EuclideanClusterExtractionModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void EuclideanClusterExtractionModel::fromJson(const QJsonObject& json, QStringList& errorList)
{

    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    leafSize_ = json["leafSize"].toDouble(leafSize_);
    segMaxIterations_ = json["segMaxIterations"].toInt(segMaxIterations_);
    segDistanceThreshold_ = json["segDistanceThreshold"].toDouble(segDistanceThreshold_);
    filterPercent_ = json["filterPercent"].toInt(filterPercent_);
    ecClusterTolerance_ = json["ecClusterTolerance"].toDouble(ecClusterTolerance_);
    ecMinClusterSize_ = json["ecMinClusterSize"].toInt(ecMinClusterSize_);
    ecMaxClusterSize_ = json["ecMaxClusterSize"].toInt(ecMaxClusterSize_);
}

QJsonObject EuclideanClusterExtractionModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("leafSize", QJsonValue(leafSize_));
    json.insert("segMaxIterations", QJsonValue(segMaxIterations_));
    json.insert("segDistanceThreshold", QJsonValue(segDistanceThreshold_));
    json.insert("filterPercent", QJsonValue(filterPercent_));
    json.insert("ecClusterTolerance", QJsonValue(ecClusterTolerance_));
    json.insert("ecMinClusterSize", QJsonValue(ecMinClusterSize_));
    json.insert("ecMaxClusterSize", QJsonValue(ecMaxClusterSize_));

    return json;
}
